#region Copyright RenGuiYou. All rights reserved.
//=====================================================
// NeatlyFrameWork
// Author:      RenGuiyou
// Feedback: 	mailto:750539605@qq.com
//=====================================================
#endregion

using UnityEngine;

namespace Neatly.UI
{
    public class NImageBase : NMaskableGraphic, ImageInterface
    {
        [SerializeField]
        protected Sprite m_Sprite;
        public Sprite sprite
        {
            get { return m_Sprite; }
        }
        protected Sprite activeSprite
        {
            get { return sprite; }
        }
        [SerializeField]
        protected string m_SpriteName = string.Empty;
        public string SpriteName
        {
            get { return m_SpriteName; }
            set { m_SpriteName = value; }
        }
        [SerializeField]
        private bool m_UseBlur;
        public bool UseBlur
        {
            get => m_UseBlur;
            set => m_UseBlur = value;
        }
        [SerializeField]
        private bool m_UseSpritePacker;
        public bool UseSpritePacker
        {
            get => m_UseSpritePacker;
            set => m_UseSpritePacker = value;
        }
        public override Texture mainTexture
        {
            get
            {
                if (m_Sprite == null)
                {
                    if (material != null && material.mainTexture != null)
                    {
                        return material.mainTexture;
                    }
                    return s_WhiteTexture;
                }
                return m_Sprite.texture;
            }
        }

        public NImageBase()
        {
            useLegacyMeshGeneration = false;
        }

        protected override void Awake()
        {
            base.Awake();
            if (NeatlyUI.AddAtlasRefAction != null)
            {
                NeatlyUI.AddAtlasRefAction(this);
            }
        }

        protected override void OnDestroy()
        {
            if (!string.IsNullOrEmpty(m_SpriteName) && NeatlyUI.LoseAtlasRefAction != null)
            {
                NeatlyUI.LoseAtlasRefAction(this);
            }
            base.OnDestroy();
        }

        public override Material material
        {
            get
            {
                if (m_Material != null)
                    return m_Material;
                return defaultMaterial;
            }
            set { base.material = value; }
        }

        public Material GetMaterial()
        {
            return m_Material;
        }

        public void SetMaterial(Material value)
        {
            base.material = value;
        }

        public string GetSpriteName()
        {
            return SpriteName;
        }

        public void SetSprite(string spriteName, Sprite value, Material mat = null)
        {
            if (!SetPropertyUtility.SetClass(ref m_Sprite, value)) return;
            m_SpriteName = spriteName;
            SetPropertyUtility.SetClass(ref m_Material, mat);
            SetAllDirty();
        }

        public bool IsBlur()
        {
            return UseBlur;
        }

        public void SetSpriteName(string spriteName)
        {
            if (Application.isPlaying)
            {
#if UNITY_WEBGL
                NeatlyUI.SetSpriteAction(this, "empty");
#endif
                NeatlyUI.SetSpriteAction(this, spriteName);
            }
        }
    }
}